

from bs4 import BeautifulSoup
import requests
html = """
<html>
	<head>
		<title>The Dormouse's story</title>
	</head>
<body>

<p class="title" name="dromouse"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; 
	<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>
	<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
	<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>
	and they lived at the bottom of a well.

</p>

<p class="story">...</p>

</body>
"""

soup= BeautifulSoup(html,'html.parser')
print(1,soup.head.contents)
print(1,type(soup.head.contents))
#print(2,soup.head.ontents[1],soup.head.contents[0])
print(soup.head.contents[1].name)#第1个head标签的标签名、value获取
print(soup.head.contents[1].string)


print(2,soup.p.children)
for i in soup.p.children:#第1个p标签的子标签
    print(i)
    print(i.name)
    print(i.string)

for i in soup.body.descendants:
    print('----',i)


print(9,soup.body.strings)

for i in soup.body.strings:#body标签中子标签的标签值生成的列表
    if i.strip()!='':
        print('----',i)


print(11,soup.body.parent)#body标签的父标签

print(12,soup.p.parent)
print(12,soup.p.parent.parent)

print(13,soup.a)
print(13,soup.a.next_sibling)#同位数据
print(13,soup.a.next_sibling.next_sibling)

for i in soup.a.next_siblings:#所有兄弟位数据
    print('--',i)





#soup的API查找到指定标签
print(soup.find_all('a'))#通过find_all方法查找所有a标签
print(soup.find_all('a')[0].string)
print(soup.find_all('a')[1].string)
import re
for i in soup.find_all(re.compile('^a')):#通过正则获取所有a标签
    print(i.string)





print(8,soup.find_all(href=re.compile('lacie')))#正则
print(9,soup.find_all('a',limit=2))

#多标签同时查找
for i in soup.find_all(['a','b']):
    print(i)


#标签属性过滤标签
def has_class_but_no_id(tag):
    return tag.has_attr('class') and not tag.has_attr('id')
print(90,soup.find_all(has_class_but_no_id))